package com.mud.dnd.frontend.rs.filters;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mud.dnd.domain.common.DAOException;
import com.mud.dnd.ejb.UserManager;

/**
 * Servlet Filter implementation class HidingFilter
 */
@WebFilter("/HidingFilter")
public class HidingFilter implements Filter {

	@EJB
	private UserManager userService;
	
    /**
     * Default constructor. 
     */
    public HidingFilter() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		
		String username = request.getParameter("userName");
		String session = request.getParameter("sessionID");
		boolean loggedIn =false;
		
		try {
			loggedIn = userService.checkSessionForUser(username, session);
		} catch (DAOException e) {
			loggedIn=false;
		}
		if(loggedIn)
		{
			chain.doFilter(request, response);
		}
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}
